package pers.luchuan.auth.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import pers.luchuan.auth.filter.TokenLoginFilter;
import pers.luchuan.auth.filter.TokenVerifyFilter;
import pers.luchuan.auth.properties.RsaKeyProperties;
import pers.luchuan.auth.service.UserService;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService myCustomUserService;
    @Autowired
    private RsaKeyProperties prop;

    @Bean
    public BCryptPasswordEncoder myPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
            //关闭跨站请求防护
            .cors().and().csrf().disable()
            //允许不登陆就可以访问的方法，多个用逗号分隔
            .authorizeRequests().antMatchers("/product").hasAnyRole("USER")
            //其他的需要授权后访问
            .anyRequest().authenticated()
            .and()
            //增加自定义认证过滤器
            .addFilter(new TokenLoginFilter(authenticationManager(), prop))
            //增加自定义验证认证过滤器
            .addFilter(new TokenVerifyFilter(authenticationManager(), prop))
            //前后端分离是无状态的，不用session了，直接禁用。
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        //UserDetailsService类
        auth.userDetailsService(myCustomUserService)
            //加密策略
            .passwordEncoder(myPasswordEncoder());

    }
}